require(['jquery',
         'global',
         'module/util',
         'bootbox.min',
		 'underscore',
		 'page/isbnmanage/editor/isbn_information_module',
		 'page/isbnmanage/editor/isbn_order_module',
		 'page/isbnmanage/editor/isbn_page_module',
         'bootstrap-datetimepicker.zh-CN',
         'bootstrap-datetimepicker',
         'bootstrap-fileinput',
         'module/ajax',
         'jquery-weui',
         'bootstrap',
         'pixeladmin'], 
function($, global, util, bootbox, _, IsbnInformation, IsbnOrder, IsbnPage){
	window.PixelAdmin.start();
	
	var barcode = $("#barcode").val();
	var isbnInformationId = $("#isbnInformationId").val();
	
	var doLoad = function() {
		doGetData({
			barcode : barcode,
			isbnInformationId : isbnInformationId,
			beforeSend : function() {
				$("#isbnLoding").show();
			},
			success : function(result) {
				this['data'] = result.data;
				doLoadData(this);
			},
			error : function(result) {
				$.alert("加载ISBN信息出现错误，请重试！");
			}, 
			complete : function(result) {
				$("#isbnLoding").hide();
				$("#isbnPanel").show();
			}
		});
	}
	var doLoadData = function(obj) {
		if(obj && obj.data && obj.data.length > 0) {
			for(var i in obj.data) {
				var dto = obj.data[i];
				createHtml(dto);
			}
		}
	}
	
	var doAddData = function() {
		doGetNewOne({
			barcode : barcode,
			beforeSend : function() {
				$("#isbnLoding").show();
			},
			success : function(result) {
				if(result && result.code == "SUCCESS") {
					doLoadData({data : [result.data]});
				} else {
					$.alert(result.message);
				}
			},
			error : function() {
				$.alert("新建ISBN信息出现错误，请重试！");
			}, 
			complete : function(result) {
				$("#isbnLoding").hide();
			}
		});
	}
	
	var createHtml = function(obj) {
		var index = $(".isbn").length;
		var render = _.template($("#isbnTemplate").html());
		var renderHtml = render({index : index, id : obj.id, barcode : obj.barcode});
		$("#isbnBody").append(renderHtml);
		
		IsbnInformation.doLoad({
			barcode : obj.barcode,
			isbnInformationId : obj.id,
			root : "ISBN_INFO_ROOT_" + obj.id
		});
	}
	
	var doGetNewOne = function(obj) {// 查询基本数据
		$.ajax({
	        url : global.context + '/web/isbninformation/newInformation',
	        data : util.stringify_aoData({barcode : barcode}),
            type : 'POST',
            dataType : 'json',
            contentType: 'application/json; charset=UTF-8',
            befondSend : function() {
	        	if(obj && obj.befondSend) obj.befondSend();
            },
	        success : function(result) {
		        if(obj && obj.success) obj.success(result);
	        },
	        error : function(result) {
	        	if(obj && obj.error) obj.error(result);
	        },
			complete : function(result) {
	        	if(obj && obj.complete) obj.complete(result);
			}
		});
	}	
	var doGetData = function(obj) {// 查询基本数据
		$.ajax({
	        url : global.context + '/web/isbninformation/getIsbnInformation',
	        data : {barcode : obj.barcode, isbnInformationId : obj.isbnInformationId},
            type : 'POST',
            dataType : 'json',
            beforeSend : function() {
	        	if(obj && obj.beforeSend) obj.beforeSend();
            },
	        success : function(result) {
		        if(obj && obj.success) obj.success(result);
	        },
	        error : function(result) {
	        	if(obj && obj.error) obj.error(result);
	        	$.alert("加载失败，请重试！");
	        },
			complete : function(result) {
	        	if(obj && obj.complete) obj.complete(result);
			}
		});
	}
	
	var changeIconClass = function($this) {
		$this.find(".glyphicon").each(function() {
			if($(this).hasClass("glyphicon-plus")) {
				$(this).removeClass("glyphicon-plus").addClass("glyphicon-minus");
			} else {
				$(this).removeClass("glyphicon-minus").addClass("glyphicon-plus");
			}
		});
	}
	var doLoadOrder = function($this) {
		if($this.find(".glyphicon-plus").length == 1) {
			IsbnOrder.doLoad({
				barcode : $this.attr("barcode"),
				isbnInformationId : $this.attr("info-id"),
				root : "ISBN_ORDER_ROOT_" + $this.attr("info-id"),
				success : function() {
					changeIconClass($this);
				}
			});
		} else {
			$("#ISBN_ORDER_ROOT_" + $this.attr("info-id")).html("");
			changeIconClass($this);
		}
	}

	var doLoadPage = function($this) {
		if($this.find(".glyphicon-plus").length == 1) {
			IsbnPage.doLoad({
				barcode : $this.attr("barcode"),
				isbnInformationId : $this.attr("info-id"),
				root : "ISBN_PAGE_ROOT_" + $this.attr("info-id"),
				success : function() {
					changeIconClass($this);
				}
			});
		} else {
			$("#ISBN_PAGE_ROOT_" + $this.attr("info-id")).html("");
			changeIconClass($this);
		}
	}

	var bindEvent = function() {
		$("body").on('click','#addIsbnInfo',function(){
			doAddData();
		});
		$("body").on('click','.collapse-info',function(){
			return false;
		});
		$("body").on('click','.collapse-order',function(){
			doLoadOrder($(this));
			return false;
		});
		$("body").on('click','.collapse-page',function(){
			doLoadPage($(this));
			return false;
		});
	} 
	
	// init start
	var init = function() {
		doLoad();
		bindEvent();
	};
	// init end
	init();

});